package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Candidate;
import com.ruoyi.system.domain.Ccompany;
import com.ruoyi.system.domain.SysProject;
import com.ruoyi.system.domain.SysSchool;
import com.ruoyi.system.mapper.CandidateMapper;
import com.ruoyi.system.service.ICandidateService;
import com.ruoyi.system.service.ICcompanyService;
import com.ruoyi.system.service.ISysProjectService;
import com.ruoyi.system.service.ISysSchoolService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-01
 */
@Service
public class CandidateServiceImpl implements ICandidateService 
{
    @Resource
    private CandidateMapper candidateMapper;

    @Resource
    private ICcompanyService ccompanyService;

    @Resource
    private ISysSchoolService sysSchoolService;

    @Resource
    private ISysProjectService sysProjectService;

    @Override
    public Long selectCandCount(Candidate candidate) {
        Long aLong = candidateMapper.selectCandCount(candidate);
        return aLong;
    }

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Candidate selectCandidateById(Long id)
    {
        return candidateMapper.selectCandidateById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param candidate 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
//    @DataScope(deptAlias = "d", userAlias = "u")
    public List<Candidate> selectCandidateList(Candidate candidate)
    {
        return candidateMapper.selectCandidateList(candidate);
    }

    @Override
    public List<Candidate> selectCandSearchList(Candidate candidate)
    {
        return candidateMapper.selectCandSearchList(candidate);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param candidate 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public long insertCandidate(Candidate candidate)
    {
        Long cRel = candidateMapper.insertCandidate(candidate);
        if (cRel == null) {
            throw new ServiceException("insert err");
        }
        //插入公司
        List<Ccompany> ccompanyList = candidate.getCcompanyList();
        if (ccompanyList != null && ccompanyList.size() > 0) {
            for (int i = 0; i < ccompanyList.size(); i++) {
                if (!StringUtils.isBlank(ccompanyList.get(i).getCom())) {
//                    if (!StringUtils.isBlank(ccompanyList.get(i).getStartTime())) {
//                        ccompanyList.get(i).setStartTime(ccompanyList.get(i).getStartTime() + "-01 00:00:00");
//                    }
                    ccompanyList.get(i).setCid(candidate.getId());
                    if(ccompanyService.insertCcompany(ccompanyList.get(i)) <= 0){
                        throw new ServiceException("insert err");
                    }
                }
            }
        }

        //插入项目
        List<SysProject> proList = candidate.getProList();
        if (proList != null && proList.size() > 0) {
            for (int i = 0; i < proList.size(); i++) {
                if (!StringUtils.isBlank(proList.get(i).getTitle())) {
                    proList.get(i).setCid(candidate.getId());
                    if(sysProjectService.insertSysProject(proList.get(i)) <= 0){
                        throw new ServiceException("insert err");
                    }
                }
            }
        }

        //插入学校
        List<SysSchool> schoolList = candidate.getSchoolList();
        if (schoolList != null && schoolList.size() > 0) {
            for (int m = 0; m < schoolList.size(); m++) {
//                if (!StringUtils.isBlank(schoolList.get(m).getStartTime())) {
//                    schoolList.get(m).setStartTime(schoolList.get(m).getStartTime() + "-01 00:00:00");
//                }
//                if (!StringUtils.isBlank(schoolList.get(m).getEndTime())) {
//                    schoolList.get(m).setEndTime(schoolList.get(m).getEndTime() + "-01 00:00:00");
//                }
                schoolList.get(m).setUserId(candidate.getId());
                if (!StringUtils.isBlank(schoolList.get(m).getName())) {
                    if(sysSchoolService.insertSysSchool(schoolList.get(m)) <= 0){
                        throw new ServiceException("insert err");
                    }
                }
            }
        }
        return 1L;
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param candidate 【请填写功能名称】
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCandidate(Candidate candidate)
    {
        int cRel = candidateMapper.updateCandidate(candidate);
        if (cRel <= 0) {
            throw new ServiceException("update err");
        }
        //更新工作经验
        List<Ccompany> ccompanyList = candidate.getCcompanyList();
        if (ccompanyList != null && ccompanyList.size() > 0) {
            for (int i = 0; i < ccompanyList.size(); i++) {
                if (StringUtils.isBlank(ccompanyList.get(i).getCom())) {
                    continue;
                }
                Long id = ccompanyList.get(i).getId();
//                if (!StringUtils.isBlank(ccompanyList.get(i).getStartTime())) {
//                    ccompanyList.get(i).setStartTime(ccompanyList.get(i).getStartTime() + "-01 00:00:00");
//                }
                if (id != null) {
                    ccompanyService.updateCcompany(ccompanyList.get(i));
                } else {
                    ccompanyList.get(i).setCid(candidate.getId());
                    ccompanyService.insertCcompany(ccompanyList.get(i));
                }
            }
        }

        //更新项目经验
        List<SysProject> proList = candidate.getProList();
        if (proList != null && proList.size() > 0) {
            for (int i = 0; i < proList.size(); i++) {
                if (StringUtils.isBlank(proList.get(i).getTitle())) {
                    continue;
                }
                Long id = proList.get(i).getId();
                if (id != null) {
                    sysProjectService.updateSysProject(proList.get(i));
                } else {
                    proList.get(i).setCid(candidate.getId());
                    sysProjectService.insertSysProject(proList.get(i));
                }
            }
        }

        //更新学校信息
        List<SysSchool> schoolList = candidate.getSchoolList();
        if (schoolList != null && schoolList.size() > 0) {
            for (int m = 0; m < schoolList.size(); m++) {
                if (StringUtils.isBlank(schoolList.get(m).getName())) {
                    continue;
                }
                String s = schoolList.get(m).getId();
//                if (!StringUtils.isBlank(schoolList.get(m).getStartTime())) {
//                    schoolList.get(m).setStartTime(schoolList.get(m).getStartTime() + "-01 00:00:00");
//                }
//                if (!StringUtils.isBlank(schoolList.get(m).getEndTime())) {
//                    schoolList.get(m).setEndTime(schoolList.get(m).getEndTime() + "-01 00:00:00");
//                }
                if (StringUtils.isBlank(s)) {
                    schoolList.get(m).setUserId(candidate.getId());
                    sysSchoolService.insertSysSchool(schoolList.get(m));
                } else {
                    sysSchoolService.updateSysSchool(schoolList.get(m));
                }
            }
        }
        return 1;
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCandidateByIds(Long[] ids)
    {
        int delRel = candidateMapper.deleteCandidateByIds(ids);
        if (delRel <= 0) {
            throw new ServiceException("del err");
        } else {
            //删除工作经历
            for (int i = 0; i < ids.length; i++) {
                Ccompany ccompany = new Ccompany();
                ccompany.setCid(ids[i]);
                List<Ccompany> ccompanyList = ccompanyService.selectCcompanyList(ccompany);
                if (ccompanyList != null && ccompanyList.size() > 0) {
                    for (int j = 0; j < ccompanyList.size(); j++) {
                        if(ccompanyService.deleteCcompanyByCid(ccompanyList.get(j).getId()) <= 0) {
                            throw new ServiceException("del err");
                        }
                    }
                }

                SysProject sysProject = new SysProject();
                sysProject.setCid(ids[i]);
                List<SysProject> proList = sysProjectService.selectSysProjectList(sysProject);
                if (proList != null && proList.size() > 0) {
                    for (int j = 0; j < proList.size(); j++) {
                        if(sysProjectService.deleteSysProjectById(
                                String.valueOf(proList.get(j).getId().longValue())) <= 0) {
                            throw new ServiceException("del err");
                        }
                    }
                }

                //删除学校
                SysSchool sysSchool = new SysSchool();
                sysSchool.setUserId(ids[i]);
                List<SysSchool> sysSchoolList = sysSchoolService.selectSysSchoolList(sysSchool);
                if (sysSchoolList != null && sysSchoolList.size() > 0) {
                    for (int m = 0; m < sysSchoolList.size(); m++) {
                        if (sysSchoolService.deleteSysSchoolById(sysSchoolList.get(m).getId()) <= 0) {
                            throw new ServiceException("del err");
                        }
                    }
                }
            }
        }
        return 1;
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCandidateById(Long id)
    {
        return candidateMapper.deleteCandidateById(id);
    }
}
